package cn.txauction.business.service.impl;


import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.txauction.business.service.IAuctionService;
import cn.txauction.business.service.base.BaseService;
import cn.txauction.common.AppUtils;
import cn.txauction.common.Constants;
import cn.txauction.modal.TxAuction;
import cn.txauction.modal.TxAuctionrecord;
import cn.txauction.modal.TxItem;

/**
 * 
 * @author 胡
 *
 */
@Service("auctionService")
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public class AuctionServiceImpl extends BaseService implements
IAuctionService {
	
	@Transactional
	public TxAuctionrecord auctionItem(TxItem item, Long memberid) {
		//获取竞拍商品
		TxItem curitem = itemDao.get(item.getFdId());
		
		//获取该商品竞拍管理信息
		TxAuction txAuction = auctionDao.findByItemId(curitem);
		
		//设置商品竞拍记录
		TxAuctionrecord auctionRecord = new TxAuctionrecord();
		auctionRecord.setTxItem(curitem);
		auctionRecord.setFdTime(new Date());
		auctionRecord.setFdMemberid(memberid);
		
		//设置竞拍记录的当前竞价，如果已有竞价记录，+step，如果没有竞价记录，
		//则在起拍价上+step
		List<TxAuctionrecord> auctionRecordList = auctionrecordDao.findByItemAndUser(curitem, memberid);
		if(auctionRecordList!=null && auctionRecordList.size() > 0){
			auctionRecord.setFdCurprice(auctionRecord.getFdCurprice()
					+ txAuction.getFdStep());
		}else{
			auctionRecord.setFdCurprice(txAuction.getFdFirstprice()
					+ txAuction.getFdStep());
		}
		
		//判断商品是否竞拍成功
		boolean auctionSuccess = false;
		int i = AppUtils.compareDate(new Date(),txAuction.getFdEndtime());
		if(i!=2){
			auctionSuccess = true;
		}
		byte islast = auctionSuccess?Constants.ConstKeys.AUCTIONSUCCESS:Constants.ConstKeys.AUCTIONING;
		auctionRecord.setFdIslast(islast);
		
		auctionrecordDao.save(auctionRecord);
		
		//如果竞拍成功，生成订单
		
		
		return auctionRecord;
	}
	
	
	
}
